package FIT::Util::ClassLoader;

use FIT::Class;
use Moose::Util::TypeConstraints;
use aliased 'Symbol::Table';

sub load() {
	my ($class, $package) = @_;
    return if package_defined($package);
    eval "use $package";
	die "Can't load package [$package]: $@" if $@;
}

# only works for Moose derived classes
sub create() {
    my ($class, %args) = @_;
    local $_;
    my ($name, $base, $methods) = map { $args{$_} } qw(name base methods);
    $methods = {}      unless defined $methods;
    $base    = 'Moose' unless defined $base;
    return if package_defined($name);

    $class->load($base);
    Table->New('ARRAY', $name)->{ISA} = [$base];
    subtype $name => as 'Object' => where { $_->isa($name) };

    for my $method (keys %$methods)
        { Table->New('CODE', $name)->{$method} = $methods->{$method} }
}

sub package_defined() { no warnings; defined *{shift . '::'} }
